/**
 * 
 */
package vashion.azeroth.core.manager.item;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import vashion.azeroth.contant.AzerothConstants;
import vashion.azeroth.contant.GenericErrorEnum;
import vashion.azeroth.contant.TagErrorEnum;
import vashion.azeroth.core.common.NeedOperateFlow;
import vashion.azeroth.core.dao.BaseDao;
import vashion.azeroth.core.item.dao.TagDao;
import vashion.azeroth.core.item.pojo.ForestDO;
import vashion.azeroth.core.item.pojo.ItemBrandDO;
import vashion.azeroth.core.item.pojo.TagDO;
import vashion.azeroth.core.manager.forest.BaseForestManager;
import vashion.azeroth.core.manager.impl.BaseManagerImpl;
import vashion.azeroth.core.pojo.common.OperateFlowEnum;
import vashion.azeroth.core.result.ApiResult;

/**
 * @author Reganchen on 17/7/17. 商品的标签管理
 */
@Service
public class TagManager extends BaseManagerImpl<TagDO> {

	private static final String ITEM_FEMALE = "女";

	private static final String ITEM_MALE = "男";

	private static final String ITEM_ALL = "通用";

	private final static Logger log = LoggerFactory.getLogger(TagManager.class);

	@Autowired
	private TagDao tagDao;

	@Autowired
	private BaseItemBrandManager baseItemBrandManager;

	@Autowired
	private BaseForestManager baseForestManager;

	@Override
	public BaseDao<TagDO> getDao() {
		return tagDao;
	}

	/**
	 * 按照标题模糊查找可用的标签
	 * 
	 * @param tagTitle
	 * @return 符合查询条件的标签
	 */
	public ApiResult<List<TagDO>> findByTagTitle(String tagTitle) {
		try {
			TagDO tagDO = new TagDO();
			tagDO.setTitle(tagTitle);
			tagDO.setStatus(AzerothConstants.ItemTag.NORMAL);
			List<TagDO> tagList = tagDao.find(tagDO);
			return ApiResult.newSuccessResult(tagList);
		} catch (Exception e) {
			return ApiResult.newErrorResult(GenericErrorEnum.SYSTEM_ERROR.getErrorCode(),
					GenericErrorEnum.SYSTEM_ERROR.getErrorMessage());
		}
	}

	/**
	 * 按照标题精确查找可用的标签
	 * 
	 * @param tagTitle
	 * @return 符合查询条件的标签
	 */
	public ApiResult<List<TagDO>> findByExactTagTitle(String tagTitle) {
		try {
			TagDO tagDO = new TagDO();
			tagDO.setTitle(tagTitle);
			tagDO.setStatus(AzerothConstants.ItemTag.NORMAL);
			List<TagDO> itemLenuxForestList = tagDao.findByExactTitle(tagDO);
			return ApiResult.newSuccessResult(itemLenuxForestList);
		} catch (Exception e) {
			return ApiResult.newErrorResult(GenericErrorEnum.SYSTEM_ERROR.getErrorCode(),
					GenericErrorEnum.SYSTEM_ERROR.getErrorMessage());
		}
	}

	/**
	 * 查找所有可用的标签
	 * 
	 * @return 符合查询条件的标签
	 */
	public ApiResult<List<TagDO>> findAll() {
		try {
			TagDO tagDO = new TagDO();
			tagDO.setStatus(AzerothConstants.ItemTag.NORMAL);
			List<TagDO> tagList = tagDao.find(tagDO);
			return ApiResult.newSuccessResult(tagList);
		} catch (Exception e) {
			return ApiResult.newErrorResult(GenericErrorEnum.SYSTEM_ERROR.getErrorCode(),
					GenericErrorEnum.SYSTEM_ERROR.getErrorMessage());
		}
	}

	/**
	 * 保存一个标签
	 * 
	 * @param tagTitle
	 * @param operatorId
	 * @return 保存是否成功
	 */
	@NeedOperateFlow(operateIdArgLocation = 1, operateFlowEnum = OperateFlowEnum.TAG)
	public ApiResult<Long> publishTag(String tagTitle, Long operatorId) {
		try {
			// 查询标签是否已存在
			ApiResult<List<TagDO>> tagResult = this.findByExactTagTitle(tagTitle);
			if (tagResult.isSuccess()) {
				if (CollectionUtils.isEmpty(tagResult.getData())) {
					TagDO tagDO = new TagDO();
					tagDO.setTitle(tagTitle);
					tagDO.setStatus(AzerothConstants.ItemTag.NORMAL);
					tagDO.setOperatorId(operatorId);
					long tagId = tagDao.saveWithId(tagDO);
					return ApiResult.newSuccessResult(tagId);
				} else {
					return ApiResult.newErrorResult(TagErrorEnum.ALREADY_EXITS.getErrorCode(),
							TagErrorEnum.ALREADY_EXITS.getErrorMessage());
				}
			} else {
				return ApiResult.newErrorResult(tagResult.getErrorCode(), tagResult.getErrorMsg());
			}
		} catch (Exception e) {
			return ApiResult.newErrorResult(GenericErrorEnum.SYSTEM_ERROR.getErrorCode(),
					GenericErrorEnum.SYSTEM_ERROR.getErrorMessage());
		}
	}

	/**
	 * 添加默认的标签，品牌、类目、适用人群
	 * 
	 * @param operatorId
	 * @return 添加结果
	 */
	@NeedOperateFlow(operateIdArgLocation = 0, operateFlowEnum = OperateFlowEnum.TAG)
	public ApiResult<Boolean> publishDefaultTags(Long operatorId) {
		// 添加品牌名为默认标签
		ItemBrandDO brandQuery = new ItemBrandDO();
		brandQuery.setPageSize(10000);
		List<ItemBrandDO> brandList = baseItemBrandManager.find(brandQuery);
		for (ItemBrandDO brandTemp : brandList) {
			publishTag(brandTemp.getTitle(), operatorId);
		}

		// 设置子类目和父类目名为默认标签
		ForestDO forestQuery = new ForestDO();
		forestQuery.setPageSize(10000);
		List<ForestDO> forestList = baseForestManager.find(forestQuery);
		for (ForestDO forestTemp : forestList) {
			publishTag(forestTemp.getTitle(), operatorId);
		}

		// 设置适用人群为默认标签
		publishTag(ITEM_ALL, operatorId);
		publishTag(ITEM_MALE, operatorId);
		publishTag(ITEM_FEMALE, operatorId);

		return ApiResult.newSuccessResult(true);
	}
	
	public List<TagDO> findByItemId(Long itemId) {
		return tagDao.findByItemId(itemId);
	}

}
